import pygame


class Guid:
    """
    标识标线
    """
    def __init__(self, car):
        self.car = car
        self.screen = car.screen
        self.show_car_position = True
        pass

    def update(self):
        self.draw_car_info()
        self.draw_target_line()

    def draw_car_info(self):
        if not self.show_car_position:
            return
        
        line_color = (0, 255, 255)
        screen_rect = self.screen.get_rect()
        half_height = screen_rect.height / 2
        half_width = screen_rect.width / 2
        pygame.draw.line(
            self.screen, line_color, (half_width, 0), (half_width, half_height)
        )
        pygame.draw.line(
            self.screen, line_color, (0, half_height), (half_width, half_height)
        )

        self.draw_panel(half_width, half_height, 100, 80)
        pass

    def draw_panel(self, x, y, width, height):
        panel = self.create_panel(width, height)
        panel_rect = panel.get_rect()
        panel_rect.x = x
        panel_rect.y = y
        self.screen.blit(panel, panel_rect)
        pass

    def create_panel(self, width, height):
        fg_color = (255, 255, 255, 100)
        bg_color = (0, 255, 255, 100)
        bd_color = (0, 100, 100)
        font = pygame.font.SysFont(None, 30)

        panel = pygame.Surface((width, height), pygame.SRCALPHA)
        panel_rect = panel.get_rect()
        # 画背景
        pygame.draw.rect(
            panel,
            bg_color,
            (0, 0, width, height),
            border_top_right_radius=10,
            border_bottom_left_radius=10,
        )
        # 画边框
        pygame.draw.rect(
            panel,
            bd_color,
            (0, 0, panel_rect.width, panel_rect.height),
            1,
            border_top_right_radius=10,
            border_bottom_left_radius=10,
        )

        x_image = font.render(f"x:{int(self.car.world_x)}", True, fg_color)
        x_rect = x_image.get_rect()
        x_rect.left = 5
        x_rect.top = 5
        panel.blit(x_image, x_rect)

        y_image = font.render(f"y:{int(self.car.world_y)}", True, fg_color)
        y_rect = y_image.get_rect()
        y_rect.left = x_rect.left
        y_rect.top = x_rect.bottom + 5
        panel.blit(y_image, y_rect)

        angle_image = font.render(f"d:{int(self.car.world_angle)}", True, fg_color)
        angle_rect = angle_image.get_rect()
        angle_rect.left = y_rect.left
        angle_rect.top = y_rect.bottom + 5
        panel.blit(angle_image, angle_rect)

        return panel

    def draw_target_line(self):
        if not self.car.has_target:
            return
        line_color = (0, 0, 0)
        car_win_x = self.car.worldx_to_winx(self.car.world_x)
        car_win_y = self.car.worldy_to_winy(self.car.world_y)
        target_win_x = self.car.worldx_to_winx(self.car.target.world_x)
        target_win_y = self.car.worldy_to_winy(self.car.target.world_y)
        pygame.draw.line(
            self.screen, line_color, (car_win_x, car_win_y), (target_win_x, target_win_y)
        )
        pass